package
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.Dictionary;
	
	public class hyEbayAjaxBridge extends Sprite
	{
		
		private var jsonStack:Dictionary = new Dictionary();
		private var xmlStack:Dictionary = new Dictionary();
		public static const JsIsReady:String = 'e$.is_ready';
		public function hyEbayAjaxBridge()
		{
			try{
				var res:* =ExternalInterface.call(JsIsReady) ;
			}catch(e:Error){
				trace(e);
			}
			this.ajax('http://localhost/ebayhelper/org/hyOnline/ebayhelper/html/sampleJSON.json',"this is first");
			
		}
		public function ajax(url:String,callback:String,params:Object=null,method:String="post",value:String="json"):void{
			var loader:URLLoader = new URLLoader();
			var vars:URLVariables = new URLVariables();
			if(params!=null){
				for ( var name:String in params){
					vars[name] = params[name];
				}
			}
			configureListeners(loader);
			var req:URLRequest = new URLRequest(url);
			if(method=="post"){
				req.method = URLRequestMethod.POST;
			}else{
				req.method = URLRequestMethod.GET;
			}
			req.data = vars;
			var data:Object = new Object();
			data.url = url;
			data.callback = callback;
			data.params = params;
			data.method = method;
			data.value = value;
			if(value == "json"){
				jsonStack[loader] = data;
			}else{
				xmlStack[loader] = data;
			}
			try {
				loader.load(req);
			} catch (error:Error) {
				trace("Unable to load requested document.");
			}
			
		}
		private function configureListeners(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
			dispatcher.addEventListener(Event.OPEN, openHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		
		private function completeHandler(event:Event):void {
			trace("load complete");
			var loader:URLLoader = URLLoader(event.target);
			var text:String = loader.data;
			if(jsonStack[loader]!=undefined){
				try{
					var data:Object = JSON.decode(text);
					ExternalInterface.call(jsonStack[loader].callback,data);
				}catch(e:Error){
					trace(e);
					trace(text);
				}
			}else if(xmlStack[loader]!=undefined){
				trace(xmlStack[loader].callback);
			}
		}
		private function openHandler(event:Event):void {
			trace("openHandler: " + event);
		}
		
		private function progressHandler(event:ProgressEvent):void {
			trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event);
		}
		
		private function httpStatusHandler(event:HTTPStatusEvent):void {
			trace("httpStatusHandler: " + event);
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void {
			trace("ioErrorHandler: " + event);
		}
	}
}